<template>
  <div class="goods-info">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!--  商品轮播图区域 -->

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <!-- :lunbotuList  :isfull都是自定义属性 传给子组件-->
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价
            <span class="old-price">￥{{goodsInfo.market_price}}</span>
            销售价
            <span class="new-price">￥{{goodsInfo.sell_price}}</span>
          </p>
          <p>
            购买数量:
            <number @getCount="getSelectedCount" :numMax="goodsInfo.stock_quantity"></number> 
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addTOShopcar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:{{goodsInfo.goods_no}}</p>
          <p>库存情况:{{goodsInfo.stock_quantity}}件</p>
          <p>上架时间:{{goodsInfo.add_time|timeFormat('YYYY-MM-DD')}}</p>
        </div>
      </div>

      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="gocomment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcompents/swiper";
import number from "../subcompents/number";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [],
      goodsInfo: {},
      ballFlag: false ,//控制小球隐藏和显示。
      selectedCount:1, //保存用户选择商品数量，默认是1
    };
  },
  methods: {
    getLunbotu() {
      this.$http.get("api/getthumimages/" + this.id).then(function(res) {
        if (res.body.status !== 0) {
          return;
        }

        res.body.message.forEach(element => {
          element.img = element.src;
        });
        this.lunbotu = res.body.message;
      });
    },
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(function(res) {
        if (res.body.status !== 0) {
          return;
        }
        this.goodsInfo = res.body.message[0];
      });
    },
    goDesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id: id } });
    },
    gocomment(id) {
      this.$router.push({ name: "goodscomment", params: { id: id } });
    },
    addTOShopcar() {
      this.ballFlag = !this.ballFlag;
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth
      const ballPosition=this.$refs.ball.getBoundingClientRect()
      const badgePostion=document.getElementById('badge').getBoundingClientRect()
      const xDist=badgePostion.left-ballPosition.left
      const yDist=badgePostion.top-ballPosition.top
      el.style.transform = `translate(${xDist}px,${yDist}px)`
      el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)"
      done()
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag
    },
    getSelectedCount(count){
      this.selectedCount=count //形参count 实参子组件number身上data的值
      console.log(this.selectedCount);
    }
  },
  created() {
    this.getLunbotu()
    this.getGoodsInfo()
  },
  components: {
    swiper,
    number
  }
}
</script>   
<style lang="scss" scoped>
.goods-info {
  background: #ccc;
  overflow: hidden; //他演
}
.price {
  .old-price {
    margin-right: 20px;
    text-decoration: line-through;
  }
  .new-price {
    color: red;
    font-size: 16px;
    font-weight: 800;
  }
}
.mui-card-footer {
  display: block;
  button {
    margin: 10px 0;
  }
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: #f00;
  position: absolute;
  z-index: 99;
  top: 390px;
  left: 143px;
 
}
</style>
